              package com.java.diagnostics.visualizer.impl.colours;
              
              import com.java.diagnostics.visualizer.data.DataSet;
              import com.java.diagnostics.visualizer.impl.GCAndMemoryVisualizerImplPreferenceInitalizer;
              import com.java.diagnostics.visualizer.impl.preferences.DataSetScope;
              import com.java.diagnostics.visualizer.impl.preferences.GCAndMemoryVisualizerPreferenceInitializer;
              import com.java.diagnostics.visualizer.impl.preferences.SmartPreferences;
              import com.java.diagnostics.visualizer.properties.OutputProperties;

          //import java.awt.Color;
              import java.util.ArrayList;
              import java.util.Collection;
              import java.util.HashMap;
              import java.util.Iterator;
              import java.util.List;
              import java.util.Set;
              import org.eclipse.core.runtime.preferences.IScopeContext;
              import org.eclipse.jface.resource.StringConverter;
              import org.eclipse.swt.graphics.Color;
              import org.eclipse.swt.graphics.Device;
              import org.eclipse.swt.graphics.GC;
              import org.eclipse.swt.graphics.RGB;
              import org.osgi.service.prefs.Preferences;
              
              public class Colours
              {
                private static final String COLOUR_MAPPINGS = "COLOUR_MAPPINGS";
                private static final String LINESTYLE_MAPPINGS = "LINESTYLE_MAPPINGS";
                private static final String CUSTOM_COLOUR_MAPPINGS = "CUSTOM_COLOUR_MAPPINGS";
                private static final int DARK = 175;
                private static final int lightGrey = 220;
                private static final int darkGrey = 120;
                private Device display;
                private SmartPreferences prefs;
                private static List fields;
                private static List variants;
                private HashMap<String, String> customLabelRGBMap = null;
                private Color[] colours;
                private HashMap<String, Color> customColours = null;
              
                private static final int[] linestyles = { 1, 3, 
                  2 };
                private Color alternateTickColour;
                private Color alternateLabelColour;
                private static final String COLOURS_PREFERENCE = "Colours";
                public static final String NUM_COLOURS = "NumColours";
                public static final int MAX_COLOURS = 20;
                public static final int MIN_COLOURS = 1;
                private static final String[] NAMES = new String[20];
                private int numVariants;
                private static final RGB[] DEFAULT_COLOUR_DEFINITIONS = { 
                  new RGB(0, 175, 175), new RGB(0, 0, 255), new RGB(255, 0, 0), 
                  new RGB(50, 175, 50), new RGB(255, 0, 255), new RGB(255, 192, 203), new RGB(173, 216, 230), new RGB(165, 42, 42), new RGB(30, 144, 255), new RGB(255, 165, 0) };
              
                static
                {
                  for (int i = 0; i < NAMES.length; i++)
                    NAMES[i] = constructPreferenceName(i);
                }
              
                public Colours(Device display, DataSet dataSet, OutputProperties outputProperties)
                {
                  IScopeContext scope = new DataSetScope(dataSet);
              
                  this.prefs = 
                    GCAndMemoryVisualizerImplPreferenceInitalizer.getInstance().getPreferences(scope, scope);
             //           deleted by wenguang.huang 
              //int numColours = this.prefs.getInt("NumColours");
             //          added by wenguang.huang 
              int numColours = 5;
              //added end by wenguang.huang 


                  update(dataSet, outputProperties);
                  this.colours = new Color[numColours];
                  this.display = display;
              
                  if (fields == null) {
                    fields = (ArrayList)SmartPreferences.unserialize(this.prefs
                      .getString("COLOUR_MAPPINGS"));
                    if (fields == null) {
                      fields = new ArrayList();
                    }
                  }
                  if (variants == null) {
                    variants = (ArrayList)SmartPreferences.unserialize(this.prefs
                      .getString("LINESTYLE_MAPPINGS"));
                    if (variants == null) {
                      variants = new ArrayList();
                    }
                  }
                  for (int i = 0; i < this.colours.length; i++) {
                    RGB rgb = StringConverter.asRGB(this.prefs.getString(preferenceName(i)));
                    this.colours[i] = new Color(display, rgb);
                  }
              
                  this.alternateTickColour = new Color(display, 220, 220, 
                    220);
                  this.alternateLabelColour = new Color(display, 120, 120, 
                    120);
              
                  if (this.customLabelRGBMap == null) {
                    this.customLabelRGBMap = ((HashMap)SmartPreferences.unserialize(
                      this.prefs.getString("CUSTOM_COLOUR_MAPPINGS")));
                    if (this.customLabelRGBMap == null) {
                      this.customLabelRGBMap = new HashMap();
                    }
              
                  }
              
                  if (this.customLabelRGBMap != null) {
                    this.customColours = new HashMap();
                    Set list = this.customLabelRGBMap.keySet();
                    Iterator it = list.iterator();
                    while (it.hasNext()) {
                      String label = (String)it.next();
                      String customRGB = (String)this.customLabelRGBMap.get(label);
                      this.customColours.put(label, new Color(display, StringConverter.asRGB(customRGB)));
                    }
                  }
                }
              
                public void adjustLineColourAndStyle(GC gc, String variant, String label) {
                  if (label != null) {
                    Color colour = getLabelColor(label);
                    gc.setForeground(colour);
                  }
                  if ((variant != null) && 
                    (this.numVariants > 1))
                  {
                    gc.setLineStyle(getLinestyle(variant));
                  }
                }
              
                public RGB getLabelRGB(String label)
                {
                  return getLabelColor(label).getRGB();
                }
              
                private Color getLabelColor(String label)
                {
                  Color labelColour = null;
              
                  if (this.customColours != null) {
                    labelColour = (Color)this.customColours.get(label);
                  }
              
                  if (labelColour == null)
                  {
                    if (!fields.contains(label)) {
                      fields.add(label);
                    }
              
                    int position = fields.indexOf(label);
                    labelColour = this.colours[(position % this.colours.length)];
              
                    setCustomLabelRGB(label, labelColour.getRGB());
                  }
              
                  return labelColour;
                }
              
                private int getLinestyle(String label)
                {
                  if (!variants.contains(label)) {
                    variants.add(label);
                  }
                  int position = variants.indexOf(label);
                  return linestyles[(position % linestyles.length)];
                }
              
                public Color getAxisColour() {
                  return this.display.getSystemColor(24);
                }
              
                public Color getBackgroundColour() {
                  return this.display.getSystemColor(25);
                }
              
                public Color getTicksColour() {
                  return this.display.getSystemColor(15);
                }
              
                public Color getAlternateTicksColour() {
                  return this.alternateTickColour;
                }
              
                public Color getAlternateLabelsColour() {
                  return this.alternateLabelColour;
                }
              
                public void dispose() {
                  for (int i = 0; i < this.colours.length; i++) {
                    this.colours[i].dispose();
                  }
              
                  if (this.customColours != null) {
                    Collection ccs = this.customColours.values();
                    Iterator it = ccs.iterator();
                    while (it.hasNext()) {
                      ((Color)it.next()).dispose();
                    }
                  }
              
                  this.alternateTickColour.dispose();
                  this.alternateLabelColour.dispose();
              
                  this.prefs.setValue("COLOUR_MAPPINGS", SmartPreferences.serialize(fields));
                }
              
                public void update(DataSet dataSet, OutputProperties outputProperties)
                {
                  this.numVariants = dataSet.getVariantLabels().length;
                }
              
                public static String preferenceName(int i) {
                  return NAMES[i];
                }
              
                private static String constructPreferenceName(int i) {
                  return "Colours" + i;
                }
              
                public static void initializeDefaults(Preferences prefs)
                {
                  prefs.putInt("NumColours", 10);
                  for (int i = 0; i < 20; i++) {
                    String colour = 
                      StringConverter.asString(DEFAULT_COLOUR_DEFINITIONS[
                      (i % 
                      DEFAULT_COLOUR_DEFINITIONS.length)]);
                    prefs.put(preferenceName(i), colour);
                  }
                  prefs.put("COLOUR_MAPPINGS", SmartPreferences.serialize(new ArrayList()));
                  prefs.put("LINESTYLE_MAPPINGS", 
                    SmartPreferences.serialize(new ArrayList()));
                  prefs.put("CUSTOM_COLOUR_MAPPINGS", 
                    SmartPreferences.serialize(new HashMap()));
                }
              
                public void setCustomLabelRGB(String label, RGB rgb)
                {
                  if (this.customLabelRGBMap == null) {
                    this.customLabelRGBMap = new HashMap();
                  }
              
                  this.customLabelRGBMap.put(label, rgb.red + "," + rgb.green + "," + rgb.blue);
              
                  this.prefs.setValue("CUSTOM_COLOUR_MAPPINGS", SmartPreferences.serialize(this.customLabelRGBMap));
              
                  this.customColours.put(label, new Color(this.display, rgb));
                }
              }

